const { home,liebiao,scan } = require("../../util/api")

// pages/shouye/shouye.js
Page({
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },

  data: {
    //视频
    v1_play:true,//默认的显示播放的按钮
    v1_pause:true,//默认不显示暂停的按钮



    indicatorDots: true,
    liebiao,
    pageNum:"",
    img:[],
    video:'',
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    deviceList:'',
    imageHight:'',
    videoHight:'',
    videos:[],
    videoIndex:0
  },
  computeImgHeight(e) {
    var winWid = wx.getSystemInfoSync().windowWidth;      //获取当前屏幕的宽度
    var imgh=e.detail.height;　　　　　　　　　　　　　　　 //图片高度
    var imgw=e.detail.width;
    var swiperH = winWid * imgh / imgw + "px"　           //等比设置swiper的高度。  
    //即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  -->  swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.setData({
      imageHight: swiperH		//设置swiper高度
    })
  },
  computeVideoHeight(e) {
    
    var winWid = wx.getSystemInfoSync().windowWidth;      //获取当前屏幕的宽度
    var imgh=e.detail.height;　　　　　　　　　　　　　　　 //图片高度
    var imgw=e.detail.width;
    var swiperH = winWid / imgw * imgh - 67.5 + "px"　           //等比设置swiper的高度。  
    //即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  -->  swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.setData({
      videoHight: swiperH		//设置swiper高度
    })
  },
  jump:function(){
 
    wx.navigateTo({
   
     url: "../shouye/peiwang/peiwang",
   
     success: function(res) {
     },
     fail: function(res) {
     },
     complete: function(res) {
     },
    })
   },
   tuwen(e){
    wx.navigateTo({
      url: '/pages/tuwen/tuwen?item='+ encodeURIComponent(JSON.stringify(e.currentTarget.dataset.item)),
   })
   
   },
   jump1(){
    wx.navigateTo({
     url: "../shouye/gongdan/gongdan",
     success: function(res) {
     },
     fail: function(res) {
     },
     complete: function(res) {
     },
    })
   
   },
   jump2(){
    wx.navigateTo({
      url:"/pages/zuyongshebei/zuyong",
    })
   },
   jum3(){
    if(wx.getStorageSync('token').length>0){
      wx.navigateTo({
        url: "/pages/lister/lister",
       })
    }else{
      wx.switchTab({
        url: '/pages/center/center',
      })
    }
   
   
   },
   renwu(){
    if(wx.getStorageSync('token').length>0){
      wx.navigateTo({
        url: '/pages/renwu/renwu',
      })
    }else{
      wx.switchTab({
        url: '/pages/center/center',
      })
    }
    
   },

   manage(){
    if(wx.getStorageSync('token').length>0){
      wx.navigateTo({
        url: '/pages/manage/manage',
      })
    }else{
      wx.switchTab({
        url: '/pages/center/center',
      })
    }
    
  },
  dianjitiaozhuan(e){
    if(wx.getStorageSync('token')=='')
    {
      wx.clearStorageSync()
      wx.setStorageSync('onshow',true )
      wx.switchTab({
        url: '/pages/center/center',
      })
  }
   let id=e.currentTarget.dataset.id
    wx.navigateTo({
     url: "../dingdan/dingdan?id="+id,
    
    })
  },
  bindended(){
   
    this.setData({
      videoIndex: this.data.videoIndex >= (this.data.videos.length - 1) ? 0 : (this.data.videoIndex +1)
    })
   
    this.setData({
      video:this.data.videos[this.data.videoIndex].ccLink
    })
  },

  

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  


  onShow(e){
    console.log(wx.getStorageSync('token'))
      home().then(res=>{
        console.log(res)
        if(res.code==200){
          for(let i=0;i< res.data.bannerList.length;i++){
            let arr=[];
            arr= res.data.bannerList[i].aaEquipmentType.etImgs.split(",")
            res.data.bannerList[i].aaEquipmentType.arr=arr;
          }
          this.setData({  
            video:res.data.video[0].ccLink,
            img:res.data.bannerList,
            deviceList:JSON.stringify(res.data.equipment),
            videos:res.data.video
          })
          
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'error',
            duration: 2000
          })
        }
    })
    let im={
      pageNum:this.data.pageNum
    }
    liebiao(im).then(res=>{
      if(res.code==200){
       console.log('res', res)
     this.setData({
       liebiao:res.data.datas
                  })
     }
    else{
      wx.showToast({
        title: res.msg,
        icon: 'error',
        duration: 2000
      })

    }
  }
     )
  },
  onLoad(e){
    let obj = wx.getLaunchOptionsSync()
    let arr=obj.query;
    if(arr.userId!=undefined){
      wx.setStorageSync('tuiguangId',arr.userId )
      let p={
        memberId:wx.getStorageSync('memberId'),
        userId:arr.userId
      }
      scan(p).then(res=>{
          if(res.code==200){
            wx.showToast({
              title: '绑定成功',
              duration: 2000,
              icon:'none'
            })
          }
      })
     
    }

    console.log(arr.userId)     
  },
   onShareAppMessage(){


    return {
      title: '分享小程序',
      imageUrl: 'https://anjingbao.anjingbao.net/profile/upload/renwufenxianginfo.jpg',
      path:"/pages/shouye/shouye",//当前页面 path ，必须是以 / 开头的完整路径
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
   
  //   wx.onAppRoute(() => {
     
  //     const pages = getCurrentPages() //获取加载的页面
  //     const view = pages[pages.length - 1] //获取当前页面的对象
  //     if (!view) return false //如果不存在页面对象 则返回
  //     view.onShareAppMessage = () => { //重写分享配置
  //       return {
  //         title: '分享的标题',
  //         imageUrl: '/assets/image/anjingbao.jpg',
  //         path: '/pages/shouye/shouye' //点击分享链接进入指定页面
  //       }
  //     }
  //     // }
  //   })
 },














 //控制视频播放
 tobePlay(e){
  let {type}=e.currentTarget.dataset;
  let data_va1='',data_va2="";

  if(type=="v1"){
    wx.createVideoContext('myVideo0').play();
    data_va1='v1_play';
    data_va2="v1_pause";
  }

  setTimeout(()=>{
    this.setData({
      [data_va1]:false,
      [data_va2]:false,
    },setTimeout(()=>{
      this.setData({
        [data_va2]:true,
      })
    },1000))
  },100)
},
//显示播放的按钮
showPlaybtn(e){
  let {type}=e.currentTarget.dataset;
  let data_va='';

  if(type=="v1"){
    if(this.data.v1_play){
      return false
    }
    data_va='v1_pause'
  }
  this.setData({
    [data_va]:false,
  },setTimeout(()=>{
    this.setData({
      [data_va]:true,
    })
  },1000))
},
//暂停视频的播放
tobePause(e){
  let {type}=e.currentTarget.dataset;
  let data_va1='',data_va2="";
  
  if(type=="v1"){
    wx.createVideoContext('myVideo0').pause();
    data_va1="v1_play";
    data_va2="v1_pause";
  }

  setTimeout(()=>{
    this.setData({
      [data_va1]:true,
      [data_va2]:true,
    })
  },100)
},

//视频播放到末尾触发的事件 视频播放结束 显示暂停的按钮
showEndBtn(e){
  //显示暂停按钮 隐藏播放按钮
  let {type}=e.currentTarget.dataset;
  let data_va='',data_va1="";
  if(type=="v1"){
    data_va="v1_play",
    data_va1="v1_pause"
  }
  this.setData({
    [data_va]:true,
    [data_va1]:true,
  })
}
})
  



  
 

 
